package com.fz.song.myspring.songfazhun;

import com.fz.song.myspring.spring.BeanPostProcessor;
import com.fz.song.myspring.spring.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class SongFaZhunBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(String beanName, Object bean) {
        if (bean instanceof UserService) {
        }

        if (bean instanceof OrderService) {
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(String beanName, Object bean) {
        if (bean instanceof UserService) {
            Object proxy = Proxy.newProxyInstance(SongFaZhunBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), (proxy1, method, args) -> {
                System.out.println("调用所有被aop拦截方法,这里就不写了.大概就是这个意思");
                return method.invoke(bean, args);
            });
            return proxy;
        }
        return bean;
    }
}
